package com.lengtouzai.databaseexp.view.friendgroup;

import com.lengtouzai.databaseexp.entity.Friend;
import com.lengtouzai.databaseexp.entity.FriendGroup;
import com.lengtouzai.databaseexp.entity.User;
import com.lengtouzai.databaseexp.view.main.MainView;
import com.lengtouzai.databaseexp.view.selectfriend.SelectFriendView;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.router.Route;
import io.jmix.core.DataManager;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.flowui.DialogWindows;
import io.jmix.flowui.component.textfield.TypedTextField;
import io.jmix.flowui.kit.component.button.JmixButton;
import io.jmix.flowui.model.CollectionContainer;
import io.jmix.flowui.model.CollectionLoader;
import io.jmix.flowui.model.InstanceContainer;
import io.jmix.flowui.view.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

@Route(value = "friendGroups/:id", layout = MainView.class)
@ViewController("exp_FriendGroup.detail")
@ViewDescriptor("friend-group-detail-view.xml")
@EditedEntityContainer("friendGroupDc")
public class FriendGroupDetailView extends StandardDetailView<FriendGroup> {
    private static final Logger log = LoggerFactory.getLogger(FriendGroupDetailView.class);
    @ViewComponent
    private InstanceContainer<FriendGroup> friendGroupDc;
    @Autowired
    private CurrentAuthentication currentAuthentication;
    @ViewComponent
    private CollectionLoader<Friend> friendsDl;
    @ViewComponent
    private TypedTextField<Object> nameField;
    @ViewComponent
    private CollectionContainer<User> friendsDc;
    @ViewComponent
    private JmixButton addFriendBtn;
    @Autowired
    private DialogWindows dialogWindows;
    @Autowired
    private DataManager dataManager;

    @Subscribe
    public void onReady(final ReadyEvent event) {
        friendsDl.setParameter("belongs", friendGroupDc.getItem());
        friendsDl.load();
        log.info("{}", friendsDc.getItems().size());
        friendGroupDc.getItem().setBelongs((User) currentAuthentication.getUser());
        if (friendGroupDc.getItem().getName() == null) return;
        nameField.setReadOnly(friendGroupDc.getItem().getName().equals("Default Group"));
        addFriendBtn.setEnabled(!nameField.isReadOnly());
    }

    @Subscribe(id = "addFriendBtn", subject = "clickListener")
    public void onAddFriendBtnClick(final ClickEvent<JmixButton> event) {
        DialogWindow<SelectFriendView> window = dialogWindows.view(this, SelectFriendView.class).build();
        window.getView().setFriendGroup(friendGroupDc.getItem());
        window.setHeight("75%");
        window.setWidth("75%");

        window.addAfterCloseListener(e -> {
            if (e.closedWith(StandardOutcome.SAVE)) {
                User selected = window.getView().getSelected();
                // get new friend
                Friend friend = dataManager.load(Friend.class)
                        .query("select f from exp_Friend f where f.user = :user and f.target = :target")
                        .parameter("user", currentAuthentication.getUser())
                        .parameter("target", selected)
                        .one();
                friend.setBelongs(friendGroupDc.getItem());
                dataManager.save(friend);
                // add to dataGrid
                friendsDl.load();
            }
        });

        window.open();
    }
}